<?php

class Debug {

	const NOT_A_VAR = 'not-a-var-value';

	public static function stop() {
	    throw new Exception('STOP');
	}

	private static function isDebugMode() {
	    return Settings::config('system')->get('debug', 'is_debug_mode');
	}

	private static function beginBlock($comment) {
        echo Env::isConsoleCall() ?
            "\n### DEBUG: $comment\n" :
            "<pre style='font-size:14px; padding:.5em 1em'>DEBUG: <b>$comment</b>\n";
	}

	private static function endBlock() {
        echo Env::isConsoleCall() ?
            "\n-----\n\n" :
            '</pre>';
	}

	public static function show($comment = 'empty', $var = self::NOT_A_VAR) {

	    if (!self::isDebugMode()) return;
		self::beginBlock($comment);

		if ($var !== self::NOT_A_VAR) {

			if (is_array($var) || is_object($var)) {
				print_r($var);
			} else {
				var_dump($var);
			}
		}
		echo "\n" . Debug::getCallPoint();
		self::endBlock();
	}

    public static function showDump($comment, $var) {

	    if (!self::isDebugMode()) return;
        self::beginBlock($comment);

        if ($var !== self::NOT_A_VAR) {
            var_dump($var);
        }
        echo "\n" . Debug::getCallPoint();

        self::endBlock();
    }

    public static function getCallPoint($level = 1) {
        $arr = debug_backtrace();
        $hCall = $arr[$level];
        return "at $hCall[file] ($hCall[line])";
    }

    public static function showTrace() {

	    if (!self::isDebugMode()) return;
        self::beginBlock('trace');

        debug_print_backtrace();
        echo "\n" . Debug::getCallPoint();

        self::endBlock();
    }

}